<?php
/**
 * @title 741. 摘樱桃 - 理解错误，只能来回各1次，不是可以反复来回走
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[][] $grid
     * @return Integer
     */
    function cherryPickup($grid) {
        /**
         * @since 2024-05-06 能摘到的最多樱桃数，起点不会是-1
         * @since 2024-05-06 矩阵边长
         */
        $maxPickup = $grid[0][0];
        $n = count($grid);

        /**
         * @since 2024-05-06 BFS广度遍历
         */
        $depth = 0;
        $nodeArray = [[0, 0]];
        while($nodeArray){
            /**
             * @since 2024-05-06 下次遍历元素的集合
             */
            $nextNodeArray = [];

            /**
             * @since 2024-05-06 遍历当前元素集合
             */
            foreach ($nodeArray as list($x, $y)){
                /**
                 * @since 2024-05-06 下次遍历的元素
                 */
                if($x+1 < $n AND $grid[$x+1][$y] != -1){
                    $maxPickup += $grid[$x+1][$y];
                    $grid[$x+1][$y] = 0;
                    $k = ($x+1) ."-{$y}";
                    $nextNodeArray[$k] = [$x+1, $y];
                } else {}
                if($y+1 < $n AND $grid[$x][$y+1] != -1){
                    $maxPickup += $grid[$x][$y+1];
                    $grid[$x][$y+1] = 0;
                    $k = "{$x}-". ($y+1);
                    $nextNodeArray[$k] = [$x, $y+1];
                } else {}
            }

            /**
             * @since 2024-05-06 下次遍历
             */
            $nodeArray = $nextNodeArray;
            $depth++;
        }

        /**
         * @since 2024-05-06 BFS深度不到2*n-1，没有到终点
         * @return
         */
        if($depth != 2*$n-1) $maxPickup = 0;
        return $maxPickup;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202405/2024.05.06.php
 */
$datas = [
    // [[[0,1,-1],[1,0,-1],[1,1,1]]], // 5
    // [[[1,1,-1],[1,-1,1],[-1,1,1]]], // 0
    [[[0,1,1,0,0],[1,1,1,1,0],[-1,1,1,1,-1],[0,1,1,1,0],[1,0,-1,0,0]]], // 11
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    /**
     * @since 2024-01-15 超时和错误案例参数
     */
    // $data = $xhprof->FormatArgs("/tmp/leetcode/xxx-xxx.txt");

    // $data[0] = $xhprof->listNode($data[0]);
    // $data[0] = $xhprof->tree($data[0]);
    echo $xhprof->DumpArgs($data);
    echo "\n<hr>\n";

    $obj = new Solution();
    $result = $obj->cherryPickup(...$data);
    echo $xhprof->DumpArgs([$result]);
    var_dump($result);

    echo "\n<hr>". str_repeat("<br>", 2) . str_repeat(PHP_EOL, 5);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
